package com.sbkj.shunbaowallet.mvp_lvxingxing.framework.mvp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.sbkj.shunbaowallet.mvp_lvxingxing.framework.annotation.CreateModel;

/**
 * @author lvxingxing
 */
public class BaseMvpPresenter<V extends IBaseMvpView, M extends IBaseMvpModel> {

    /**
     * V层view
     */
    private V mView;
    private M mModel;

    /**
     * Presenter被创建后调用
     *
     * @param savedState 被意外销毁后重建后的Bundle
     */
    public void onCreatePersenter(@Nullable Bundle savedState) {
        Log.e("perfect-mvp", "P onCreatePersenter = ");
    }

    /**
     * 绑定View
     */
    public void onAttachMvpView(V mvpView) {
        mView = mvpView;
        Log.e("perfect-mvp", "P onResume");
        //在绑定视图的同时，把model创建出来
        CreateModel annotation = getClass().getAnnotation(CreateModel.class);
        Class<M> aClass = null;
        if (annotation != null) {
            aClass = (Class<M>) annotation.value();
        }
        try {
            mModel = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    /**
     * 解除绑定View
     */
    public void onDetachMvpView() {
        mView = null;
        Log.e("perfect-mvp", "P onDetachMvpView = ");
    }

    /**
     * Presenter被销毁时调用
     */
    public void onDestroyPersenter() {
        Log.e("perfect-mvp", "P onDestroy = ");
    }

    /**
     * 在Presenter意外销毁的时候被调用，它的调用时机和Activity、Fragment、View中的onSaveInstanceState
     * 时机相同
     *
     * @param outState
     */
    public void onSaveInstanceState(Bundle outState) {
        Log.e("perfect-mvp", "P onSaveInstanceState = ");
    }

    /**
     * 获取V层接口View
     *
     * @return 返回当前MvpView
     */
    protected V getMvpView() {
        return mView;
    }

    /**
     * 获取M层model
     *
     * @return 返回当前的model
     */
    protected M getModel() {
        return mModel;
    }
}